/*
* Authors: Karymov Anton
*          Skorodumov Kirill
* This file contains additional function.
* June-August,	2011
*/


#include <boost/graph/graphviz.hpp>
#include "types.h"	
#include "graph_reader.h"

using namespace boost;
using namespace std;

graph_t read_graph(string fname){
  graph_t graph(0);
  dynamic_properties dp;

  property_map<graph_t, boost::vertex_name_t>::type name =
    get(vertex_name, graph);
  dp.property("node_id",name);

  std::fstream ifile;

  ifile.open(fname.c_str(),std::ios::in);	
  if(!ifile)
  {
    std::cerr << "Error: cannot open the file. \n";
    ifile.close();
  }
  try{
    read_graphviz(ifile,graph,dp,"node_id");
  }
  catch(bad_parallel_edge)
  { 
    std::cout<<"Please, remove parallel edges\n";
  }
  catch(undirected_graph_error)
  { 
    std::cout<<"Tried to read an undirected graph into a directed graph.";
  }
  catch(directed_graph_error)
  { 
    std::cout<<"Tried to read a directed graph into an undirected graph.\n";
  }
  ifile.close();
  return graph;
}

string int_to_string(int number){
  string number_as_string = boost::lexical_cast<string>(number);
  return number_as_string;
}

string double_to_string(double number){
  std::ostringstream out;
  out << number;
  return out.str();
}

void print_space(int length,ofstream &file){
  for(int i=0;i!=length;i++){
    file<<" ";
  }
  file<<"|";
}